package com.travel.lib.base.retrofit;


import com.travel.lib.base.utils.TextCheckUtils;
import com.travel.lib.base.utils.ToastUtils;

import rx.Subscriber;

/**
 * Created by spc on 2017/8/10.
 */
public abstract class BaseSubscriber<T> extends Subscriber<BaseRetrofitResponse<T>> {


    @Override
    public void onNext(BaseRetrofitResponse<T> baseRetrofitResponse) {

        if (baseRetrofitResponse.getStatus().equals("0")) {
            if (TextCheckUtils.isUseable(baseRetrofitResponse.getMsg())) {
                ToastUtils.show(baseRetrofitResponse.getMsg());
            }
            statusSuccess();
            onSuccess(baseRetrofitResponse.getResult());
        } else {
            ResponseStatusErrorManager.excute(baseRetrofitResponse.getStatus(), baseRetrofitResponse.getMsg());
            statusError(baseRetrofitResponse.getStatus());
            haveError();
        }
    }

    //status不为0 的时候错误
    protected void statusError(String status) {

    }

    // （ 状态不为0 和异常错误）
    protected void haveError() {

    }

    //状态成功
    protected void statusSuccess() {
    }

    public abstract void onSuccess(T data);

    @Override
    public void onCompleted() {

    }

    @Override
    public void onError(Throwable e) {
        ResponseStatusErrorManager.exceptionHandler(e);
        haveError();
    }
}
